package com.hsteach.framework.web.service;

import com.google.common.collect.Lists;
import com.hsteach.common.core.domain.model.AuthInfo;
import com.hsteach.common.core.domain.model.Children;
import com.hsteach.common.core.domain.model.Eclass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.hsteach.common.core.domain.entity.SysUser;
import com.hsteach.common.core.domain.model.LoginUser;
import com.hsteach.common.exception.ServiceException;
import com.hsteach.common.utils.StringUtils;
import com.hsteach.system.service.ISysUserService;

/**
 * 用户验证处理
 *
 * @author ruoyi
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService
{
    private static final Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysPasswordService passwordService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private AuthService authService;

    @Override
    public UserDetails loadUserByUsername(String authStr) throws UsernameNotFoundException
    {
        SysUser user = userService.selectUserByUserName("admin");
        user.setUserName(authStr);
        LoginUser loginUser = createLoginUser(user);

        // authStr 0 accessToken, 1 appToken, 2 currentStudentId
        String[] split = authStr.split(",");
        if (split.length > 1 && StringUtils.isNotEmpty(split[1]) && StringUtils.isNotEmpty(split[2])) {
            if ("app".equals(split[0])) {
                loginUser.setAuthInfo(authService.getLoginInfo(split.length >= 2 ? split[1]: null,
                        split.length >= 3 ? split[2]: null,
                        split.length >= 4 ? split[3]: null,
                        split.length >= 5 ? split[4]: null,
                        split.length >= 6 ? split[5]: null,
                        split.length >= 7 ? split[6]: null,
                        split.length >= 8 ? split[7]: null,
                        split.length >= 9 ? split[8]: null));
            } else if ("web".equals(split[0])) {
                loginUser.setAuthInfo(authService.getWebLoginInfo(split[1], split.length >= 3 ? split[2]: null,
                        split.length >= 4 ? split[3]: null,
                        split.length >= 5 ? split[4]: null));
            } else {
                throw new ServiceException("未知登录来源");
            }
        } else {
            // todo:生成模拟token，待删除
//            String accessToken = authService.getAccessToken();
//            String appToken = authService.getAppToken();
//            AuthInfo authInfo = authService.getLoginInfo(accessToken, appToken, null);
            AuthInfo authInfo = new AuthInfo();
            authInfo.setOrgCode(split[4]);
            authInfo.setXgkToken(split[3]);
            if (split.length >= 3 && StringUtils.isNotEmpty(split[2])) {
                authInfo.setAppToken(authService.getAppToken(split[2]));
            }
            Children children = new Children();
            children.setStudentId("325969110839369728");
            children.setSchoolId("222529212281221120");
            children.setRelationship("51");
            children.setStatus(1);
            children.setDefaultBind(1);
            children.setStudentName("毛孩");
            children.setEclassName("初一(2)班");
            children.setEclassId("GZ_2023_1178b2f1b5f870343d008ae7ed449c73");
            authInfo.setCurrentStudent(children);
            authInfo.setChildren(Lists.newArrayList(children));
            authInfo.setId("206366915524431872");
            authInfo.setSex("1");
            authInfo.setName("管理员");
            authInfo.setMobile("18762000000");
            authInfo.setSchoolId("206366915868364801");
            authInfo.setJobNo("18762000000");
            authInfo.setSchoolName("测试学校");
            Eclass eclass = new Eclass();
            eclass.setEclassId("GZ_2023_1178b2f1b5f870343d008ae7ed449c73");
            authInfo.setEclasses(Lists.newArrayList(eclass));
            loginUser.setAuthInfo(authInfo);
        }
        return loginUser;
    }

    public LoginUser createLoginUser(SysUser user) {
        return new LoginUser(user.getUserId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
    }
}
